package com.bzframework.ai.langchain4j.web;

import com.bzframework.ai.langchain4j.LangChain4jAiService;
import com.bzframework.core.auth.Authorization;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @Author bingzhong
 * @Date 2025/7/29
 */
@Tag(name = "langchain4j测试")
@RestController
@RequestMapping("/langchain4j")
@RequiredArgsConstructor
@Authorization(ignore = true)
public class LangChain4jController {

    private final LangChain4jAiService langChain4jService;

    @GetMapping("/chat/simple")
    @Operation(summary = "简单对话")
    public ResponseEntity<String> chatSimple(@RequestParam(required = false, defaultValue = "请问你是谁") String message) {
        return ResponseEntity.ok(langChain4jService.chatSimpleMessage(message));
    }


    @GetMapping(value = "/chat/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @Operation(summary = "流式对话")
    public Flux<String> chatStream(@RequestParam(required = false, defaultValue = "请问你是谁") String message) {
        return langChain4jService.chatStreamMessage(message);
    }

    @GetMapping("/chat/struct")
    @Operation(summary = "结构化输出")
    public LangChain4jAiService.Report chatForReport(@RequestParam(required = false, defaultValue = "请问你是谁") String message) {
        return langChain4jService.chatForReport(message);
    }

    @GetMapping("/chat/result")
    @Operation(summary = "获取ai调用的详细结果")
    public Integer chatForResult(@RequestParam(required = false, defaultValue = "请问你是谁") String message) {
        return langChain4jService.chatForResult(message).tokenUsage().totalTokenCount();
    }

    @GetMapping("/chat/tool")
    @Operation(summary = "调用自定义tool")
    public ResponseEntity<String> chatForTool(@RequestParam(required = false, defaultValue = "请帮我计算一下 888 + 888 的和") String message) {
        return ResponseEntity.ok(langChain4jService.chatForTool2Calculate(message));
    }


}
